package com.sc.nft.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.dao.PledgeTaskInfoDao;
import com.sc.nft.dao.UserPledgeCollectionRecordsDao;
import com.sc.nft.dao.UserPledgeRecordsDao;
import com.sc.nft.entity.*;
import com.sc.nft.entity.vo.AdminPagePledgeRecordsRecordVO;
import com.sc.nft.entity.vo.AdminPledgeCollectionRecordsVO;
import com.sc.nft.entity.vo.PledgeSecretAreaDetailsVO;
import com.sc.nft.entity.vo.PledgeSecretAreaPageVO;
import com.sc.nft.enums.PledgeStatusEnum;
import com.sc.nft.enums.RewardStatusEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.PledgeTaskInfoService;
import com.sc.nft.service.PledgeTaskRewardService;
import com.sc.nft.service.UserVigorousCollectionRecordsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.sc.nft.service.UserPledgeCollectionRecordsService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 质押任务-用户质押记录服务接口实现
 *
 * @author Sun
 * @since 2023-06-12 15:37:52
 * @description 由 Mybatisplus Code Generator 创建
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class UserPledgeCollectionRecordsServiceImpl implements UserPledgeCollectionRecordsService {
    private final UserPledgeCollectionRecordsDao userPledgeCollectionRecordsDao;
    private final UserPledgeRecordsDao userPledgeRecordsDao;
    private final PledgeTaskInfoDao pledgeTaskInfoDao;
    private final UserVigorousCollectionRecordsService userVigorousCollectionRecordsService;
    private final PledgeTaskRewardService pledgeTaskRewardService;
    @Override
    public Page<AdminPagePledgeRecordsRecordVO> adminPagePledgeRecordsRecord(Long pledgeTaskId,String userTel, Integer pageNo, Integer pageSize) {
        Page<AdminPagePledgeRecordsRecordVO> page = userPledgeRecordsDao.adminPagePledgeRecordsRecord(pledgeTaskId,userTel, pageNo, pageSize);
        for (AdminPagePledgeRecordsRecordVO record : page.getRecords()) {
            AdminPledgeCollectionRecordsVO adminPledgeCollectionRecordsVO =  userPledgeCollectionRecordsDao.getCollectioInfoByUserPledgeRecordsId(record.getId());
            record.setFirstTitle(adminPledgeCollectionRecordsVO.getFirstTitle());
            record.setCount(adminPledgeCollectionRecordsVO.getCount());
            if(DateTime.now().isBefore(record.getCompleteTime())){
                record.setMoreThanHour(DateUtil.between(record.getCompleteTime(), DateTime.now(), DateUnit.HOUR));
            }else{
                record.setMoreThanHour(0L);
            }

        }
        return page;
    }

    @Override
    public Integer getCountByUserIdAndPledgeTaskIdAndStatus(Long userId, Long pledgeTaskId, PledgeStatusEnum pledgeStatus) {
        return userPledgeRecordsDao.getCountByUserIdAndPledgeTaskIdAndStatus(userId,pledgeTaskId,pledgeStatus);
    }

    @Override
    public Integer getCountByUserIdAndPledgeTaskIdAndCollectionIdAndStatus(Long userId, Long pledgeTaskId, Long pledgeTaskCollectionId, PledgeStatusEnum pledgeStatus) {
        return userPledgeRecordsDao.getCountByUserIdAndPledgeTaskIdAndCollectionIdAndStatus(userId,pledgeTaskId,pledgeTaskCollectionId,pledgeStatus);
    }

    @Override
    public Page<PledgeSecretAreaPageVO> pledgeSecretAreaPage(UserInfo user, Long pledgeTaskId,Integer pageNo, Integer pageSize) {
        return userPledgeRecordsDao.pledgeSecretAreaPage(user,pledgeTaskId,pageNo,pageSize);
    }

    @Override
    public PledgeSecretAreaDetailsVO pledgeSecretAreaDetails(UserInfo user, Long pledgeTaskId, Long userPledgeRecordsId) {

        UserPledgeRecords userPledgeRecords = userPledgeRecordsDao.getOneByUserIdAndId(userPledgeRecordsId,user.getId());
        Assert.isTrue(ObjectUtil.isNotNull(userPledgeRecords),()->new GlobalRunTimeException("质押记录不存在"));


        PledgeSecretAreaDetailsVO pledgeSecretAreaDetailsVO = BeanUtil.copyProperties(userPledgeRecords, PledgeSecretAreaDetailsVO.class);
        pledgeSecretAreaDetailsVO.setCompleteTime(DateUtil.between(DateTime.now(),userPledgeRecords.getCompleteTime(),DateUnit.HOUR));
        if(userPledgeRecords.getPledgeStatus() == PledgeStatusEnum.END_OF_PLEDGE){
            pledgeSecretAreaDetailsVO.setEndTime(userPledgeRecords.getModifyTime());
        }


        pledgeSecretAreaDetailsVO.setUserVigorousCollectionList(userPledgeCollectionRecordsDao.getPledgeCollectionIdByUserPledgeRecordsId(userPledgeRecords.getId()));
        pledgeSecretAreaDetailsVO.setPledgeTaskRewardVOList(pledgeTaskRewardService.getListByPledgeTaskCollectionIdAndPledgeTaskId(pledgeTaskId,userPledgeRecords.getPledgeTaskCollectionId()));

        return pledgeSecretAreaDetailsVO;
    }

    @Override
    public Boolean updateRewardStatusById(Long id, RewardStatusEnum rewardStatus) {
        return userPledgeRecordsDao.updateRewardStatusById(id,rewardStatus);
    }

    @Override
    public Boolean updatePledgeStatusAndRewardStatusById(Long id, PledgeStatusEnum nowStatus, PledgeStatusEnum pledgeStatus, RewardStatusEnum rewardStatus) {
        return userPledgeRecordsDao.updatePledgeStatusAndRewardStatusById(id,nowStatus,pledgeStatus,rewardStatus);
    }

    @Override
    public List<UserPledgeCollectionRecords> getListByUserIdAndUserPledgeRecordsIdAndPledgeStatus(Long userId,  Long userPledgeRecordsId, PledgeStatusEnum pledgeStatus) {
        return userPledgeCollectionRecordsDao.getListByUserIdAndUserPledgeRecordsIdAndPledgeStatus(userId,userPledgeRecordsId,pledgeStatus);
    }

    @Override
    public Boolean updatePledgeStatusByUserPledgeRecordsId(Long userPledgeRecordsId, PledgeStatusEnum pledgeStatusEnum) {
        return userPledgeCollectionRecordsDao.updatePledgeStatusByUserPledgeRecordsId(userPledgeRecordsId,pledgeStatusEnum);
    }

    @Override
    public Integer getCountByUserIdAndPledgeTaskIdAndPledgeTaskCollectionIdAndStatus(Long userId, Long pledgeTaskId, Long pledgeTaskCollectionId, PledgeStatusEnum pledgeStatus) {
        return userPledgeRecordsDao.getCountByUserIdAndPledgeTaskIdAndPledgeTaskCollectionIdAndStatus(userId,pledgeTaskId,pledgeTaskCollectionId,pledgeStatus);
    }

    @Override
    public Boolean updateSuccessForPledgeStatusById(Long id) {
        return userPledgeRecordsDao.updateSuccessForPledgeStatusById(id);
    }


}